package com.classes.attributes.logic;

import java.nio.ByteBuffer;

import com.classes.attributes.AttributeContext;
import com.classes.attributes.CodeEnum;

public class Attribute_code extends AttributeAbstract {

    public Attribute_code(short nameIndex, int attributeLength) {
        super(nameIndex, attributeLength);
    }


    @Override
    public void parse(ByteBuffer buffer) {
        //剩余长度
        int length = getAttributeLength() - 6;

        short maxStack = buffer.getShort();
        short maxLocal = buffer.getShort();
        System.out.println("==============================");
        System.out.println("maxStack=" + maxStack + "  maxLocal=" + maxLocal);
        int codeLength = buffer.getInt();
        System.out.println("codeLength=" + codeLength);
        for (int i = 0; i < codeLength; i++) {
            int code = Byte.toUnsignedInt(buffer.get());
            System.out.println("code[" + i + "]=" + CodeEnum.getCodeByCode(code));
            ;
        }

        short exceptionTableLength = buffer.getShort();

        for (int i = 0; i < exceptionTableLength; i++) {
            short startPc = buffer.getShort();
            short endPc = buffer.getShort();
            short handlePc = buffer.getShort();
            short catchType = buffer.getShort();
            System.out.println("异常： startPc=" + startPc + " endPc=" + endPc + "  handlePc=" + handlePc + " catchType=" + catchType);
        }


        short attributesCount = buffer.getShort();
        for (int i = 0; i < attributesCount; i++) {
            AttributeContext context = new AttributeContext(buffer);
            context.parse();
        }
        System.out.println("==============================");
    }


}
